﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCommon;
using System.Threading.Tasks;
using TCommon.Util;
using TRTC;
using TCommon.Enum;
using AppContext = TCommon.AppContext;
using PhilipService.Entity;
using Newtonsoft.Json;

namespace PhilipSupport.Controls.MainControl
{
    public partial class MeetingMain : UserControl
    {
        public MeetingMain()
        {
            InitializeComponent();
            btn_0.Click += Btn_Click;
            btn_1.Click += Btn_Click;
            btn_2.Click += Btn_Click;
            btn_3.Click += Btn_Click;
            btn_4.Click += Btn_Click;
            btn_5.Click += Btn_Click;
            btn_6.Click += Btn_Click;
            btn_7.Click += Btn_Click;
            btn_8.Click += Btn_Click;
            btn_9.Click += Btn_Click;
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            if (tb_meetingnumber.Text == "000000") {
                tb_meetingnumber.Text = "";
            }

            if (tb_meetingnumber.Text.Length >= 6)
                return;

            int selectStart = tb_meetingnumber.SelectionStart;
            var btn = (Button)sender;
            string number = tb_meetingnumber.Text;
            number = number.Insert(selectStart, (string)btn.Tag);
            tb_meetingnumber.Text = number;
            tb_meetingnumber.SelectionStart = selectStart + 1;
        }

        private void btn_remove_Click(object sender, EventArgs e)
        {
            int selectStart = tb_meetingnumber.SelectionStart;
            if (selectStart == 0)
                return;

            string number = tb_meetingnumber.Text;
            number = number.Remove(selectStart-1, 1);
            tb_meetingnumber.Text = number;
            tb_meetingnumber.SelectionStart = selectStart - 1;
            if (tb_meetingnumber.Text == "")
            {
                tb_meetingnumber.Text = "000000";
            }
        }

        private void MeetingMain_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(AppContext.LocalRoomId))
            {
                Task.Factory.StartNew(new Action(() =>
                {
                    ulong zoomId = 0;
                    string meetingId = string.Empty;
                    string ownId = string.Empty;

                    zoomId = ulong.Parse(PhilipContext.OwnId);
                    ownId = PhilipContext.HostId;
                    var name = StringUtil.ToHex(string.Format("{0}:{1}", ownId, zoomId));
                    AppContext.LocalUserName = name;
                    TCommon.AppContext.CurrentUser = name;
                    var manager = new ServiceManager();
                    AppContext.UserSign = manager.GetUserSign((int)zoomId, name);
                    TCommon.AppContext.LocalRoomId = zoomId.ToString();
                    Invoke(new Action(() =>
                    {
                        label_localMeetingNumber.Text = AppContext.LocalRoomId;
                    }));
                }));
            }
            else
            {
                label_localMeetingNumber.Text = AppContext.LocalRoomId;
            }
        }

        private void btn_HostMeeting_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(AppContext.LocalRoomId))
            {
                MessageBox.Show("正在获取会议号，请稍后再试");
                return;
            }

            var form = new VideoMainForm(int.Parse(AppContext.LocalRoomId), AppContext.LocalUserName, AppContext.LocalUserName, AppContext.UserSign, UserRole.Host);
            form.SetPhilipsMode();
            form.FormClosed += Form_FormClosed;
            form.RemoveUserClicked += Form_RemoveUserClicked;
            form.Show();
            if (PhilipContext.isAvailable)
            {
                PhilipContext.supportMain.SetOnlineStatus(Entity.OnLineStatus.Busy);
                PhilipContext.supportMain.SetStatusButtonsEnable(false);
            }
        }

        private void Form_RemoveUserClicked(string userId, long serviceId)
        {
            var req = new KickUserRequest()
            {
                Type = "kickuser",
                UserId = userId,
                ServiceId = serviceId
            };
            var msg = JsonConvert.SerializeObject(req);
            PhilipContext.supportMain.SendMsg(msg);
        }

        private void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (PhilipContext.isAvailable)
            {
                PhilipContext.supportMain.SetOnlineStatus(Entity.OnLineStatus.Online);
                PhilipContext.supportMain.SetStatusButtonsEnable(true);
            }
        }

        private void btn_joinmeeting_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_meetingnumber.Text) ||
                tb_meetingnumber.Text == "000000") {
                MessageBox.Show("请输入会议号");
                return;
            }

            var meetingId = int.Parse(tb_meetingnumber.Text);
            TCommon.AppContext.AttendRoomId = meetingId.ToString();
            var form = new VideoMainForm(null, meetingId, "", AppContext.LocalUserName, "", AppContext.UserSign, UserRole.Participant);
            form.SetPhilipsMode();
            form.FormClosed += Form_FormClosed;
            form.RemoveUserClicked += Form_RemoveUserClicked;
            form.Show();
            if (PhilipContext.isAvailable)
            {
                PhilipContext.supportMain.SetOnlineStatus(Entity.OnLineStatus.Busy);
                PhilipContext.supportMain.SetStatusButtonsEnable(false);
            }
        }
    }
}
